package com.jspx.txweb.action;

import com.jspx.boot.environment.Environment;
import com.jspx.boot.sign.OAuthOpenIdType;
import com.jspx.sioc.annotation.Ref;
import com.jspx.txweb.IUserSession;
import com.jspx.txweb.annotation.HttpMethod;
import com.jspx.txweb.annotation.Operate;
import com.jspx.txweb.annotation.Safety;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.txweb.table.OAuthOpenId;
import com.jspx.txweb.table.UserSession;
import com.jspx.txweb.util.TXWebUtil;
import com.jspx.utils.StringUtil;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.qzone.Topic;
import com.qq.connect.javabeans.GeneralResultBean;
import jspx.user.dao.JUserMemberDAO;
import jspx.user.env.JUserIoc;


@HttpMethod(caption="腾讯说说发布接口")
public class ShuoShuoAction extends ActionSupport {
    protected JUserMemberDAO memberDAO;
    @Safety(request = false)
    @Ref(name = JUserIoc.memberDAO, namespace = JUserIoc.namespace)
    public void setMemberDAO(JUserMemberDAO memberDAO) {
        this.memberDAO = memberDAO;
    }

    @Operate(caption = "发布说说")
    public void post() throws Exception {
        String con = getString("content");
        if (StringUtil.isNULL(con))
        {
            addFieldInfo(Environment.errorInfo,"没有读起到qq信息");
            return;
        }

        IUserSession userSession = (IUserSession)getUserSession();

        OAuthOpenId authOpenId = memberDAO.getOAuthOpenId(OAuthOpenIdType.qq,userSession.getUid());
        if (authOpenId==null)
        {
            addFieldInfo(Environment.errorInfo,"没有读起到qq信息");
            return;
        }
        Topic topic = new Topic(authOpenId.getAccessToken(), authOpenId.getOpenId());
        try {
            GeneralResultBean grb = topic.addTopic(con);
            if (grb.getRet() == 0) {
                TXWebUtil.print("<a href=\"http://www.qzone.com\" target=\"_blank\">您的说说已发表成功，请登录Qzone查看</a>",TXWebUtil.htmlType,response);
            } else {
                printError("发表说说失败！原因： " + grb.getMsg(),500);
            }
        } catch (QQConnectException e) {
            addFieldInfo(Environment.errorInfo,"发布异常");
        }
    }
}
